package com.nielsenedu.array2d.CandyBox;

public class CandyBox {

	private static final int CELL_WIDTH = 10;

	/** box contains at least one row and is initialized in the * constructor */
	Candy[][] box;

	public CandyBox(int rows, int columns) {
		box = new Candy[rows][columns];
	}

	/**
	 * Moves one piece of candy in column col, if necessary and possible,
	 * so that the box element in row 0 of column col contains a piece of
	 * candy, as described in part (a).
	 * Returns false if there is no piece of candy in column col and
	 * returns true otherwise. 
	 * Precondition: col is a valid column index in box.
	 */
	public boolean moveCandyToFirstRow(int col) {
		/* to be implemented in part (a) */
		return true;
	}

	/**
	 * Removes from box and returns a piece of candy with flavor specified
	 * by the parameter, returns null if no such piece is found, as
	 * described in part (b)
	 */
	public Candy removeNextByFlavor(String flavor) {
		/* to be implemented in part (b) */
		return new Candy("removeNextByFlavor NOT IMPLEMENTED");
	}

	/**
	 * Place the given candy in the specified row and column of the CandyBox
	 * @param c the candy to place
	 * @param row the row in which to place the candy
	 * @param col the column in which to place the candy
	 * @return true if a candy already existed in the space, otherwise false
	 */
	public boolean placeCandy(Candy c, int row, int col) {
		boolean replace = box[row][col] != null;
		box[row][col] = c;
		return replace;
	}

	// YOU DO NOT NEED TO UNDERSTAND THE CODE BELOW THIS POINT
	// BUT YOU CAN EXAMINE IT IF YOU'RE INTERESTED. TEXT ALIGNMENT
	// IS A BIT OF A HASSLE TO DO...

	private String paddedFlavor(Candy c) {
		String flavor = "";
		if(c != null) {
			flavor = c.getFlavor();
		}
		return padString(flavor);
	}

	private String padString(String s) {
		int whitespace = CELL_WIDTH - s.length();
		int left = whitespace / 2;
		int right = whitespace - left;
		return " ".repeat(left) + s + " ".repeat(right);
	}

	private String firstRow() {
		String s = "   ";
		for(int col = 0; col < box[0].length; col++) {
			s += " " + padString(""+col);
		}
		return s + "\n";
	}

	public String toString() {
		int rowChars = (CELL_WIDTH + 1) * box[0].length + 1;
		String hr = "   " + "-".repeat(rowChars) + "\n";
		String s = firstRow() + hr;
		for(int row = 0; row < box.length; row++) {
			s += " " + row + " "; // only aligned if row is single digit
			for(Candy c : box[row]) {
				s += "|" + paddedFlavor(c);
			}
			s += "|\n" + hr;
		}
		return s;
	}

	// TESTING

	public static void testMoveCandyToFirstRow() {
		System.out.println("***************************");
		System.out.println("*** moveCandyToFirstRow ***");
		System.out.println("***************************\n");
		CandyBox b = new CandyBox(4,3);
		b.placeCandy(new Candy("lime"),   0, 1);
		b.placeCandy(new Candy("orange"), 1, 1);
		b.placeCandy(new Candy("cherry"), 2, 2);
		b.placeCandy(new Candy("lemon"),  3, 1);
		b.placeCandy(new Candy("grape"),  3, 2);
		System.out.println("Original CandyBox:");
		System.out.println(b);
		System.out.println("Testing method moveCandyToFirstRow()");
		System.out.println("   moveCandyToFirstRow(0): " +
				b.moveCandyToFirstRow(0));
		System.out.println("   moveCandyToFirstRow(1): " +
				b.moveCandyToFirstRow(1));
		System.out.println("   moveCandyToFirstRow(2): " +
				b.moveCandyToFirstRow(2));
		System.out.println("\nResulting CandyBox:\n" + b);
	}

	public static void testRemoveNextByFlavor() {
		System.out.println("**************************");
		System.out.println("*** removeNextByFlavor ***");
		System.out.println("**************************\n");
		CandyBox b = new CandyBox(3,5);
		b.placeCandy(new Candy("lime"),   0, 0);
		b.placeCandy(new Candy("lime"),   0, 1);
		b.placeCandy(new Candy("lime"),   1, 3);
		b.placeCandy(new Candy("lime"),   1, 4);
		b.placeCandy(new Candy("lemon"),  0, 3);
		b.placeCandy(new Candy("lemon"),  2, 2);
		b.placeCandy(new Candy("orange"), 1, 0);
		b.placeCandy(new Candy("orange"), 2, 4);
		b.placeCandy(new Candy("cherry"), 2, 0);
		System.out.println("Original CandyBox:");
		System.out.println(b);
		
		System.out.println("removeNextByFlavor(\"cherry\") : " +
				b.removeNextByFlavor("cherry"));
		System.out.println("\nResulting CandyBox:\n" + b);

		System.out.println("removeNextByFlavor(\"lime\") : " +
				b.removeNextByFlavor("lime"));
		System.out.println("\nResulting CandyBox:\n" + b);

		System.out.println("removeNextByFlavor(\"grape\") : " +
				b.removeNextByFlavor("grape"));
		System.out.println("\nResulting CandyBox (unchanged):\n" + b);
	}

	public static void main(String[] args) {
		testMoveCandyToFirstRow();
		testRemoveNextByFlavor();
	}

}
